package com.zq.sysuser.config;

import com.zq.sysuser.dao.entity.Permission;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {


    /**
     * 使用递归方法建菜单
     *
     * @param sysMenuList
     * @return
     */
    public static List<Permission> BuildTree(List<Permission> sysMenuList) {
        List<Permission> trees = new ArrayList<>();
        for (Permission sysMenu : sysMenuList) {
            if (sysMenu.getParentId() == 0) {
                trees.add(findChildren(sysMenu, sysMenuList));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点
     *
     * @param treeNodes
     * @return
     */
    public static Permission findChildren(Permission sysMenu, List<Permission> treeNodes) {
        sysMenu.setChildren(new ArrayList<>());
        for (Permission it : treeNodes) {
            if (sysMenu.getId().longValue() == it.getParentId().longValue()) {

                sysMenu.getChildren().add(findChildren(it, treeNodes));
            }
        }
        return sysMenu;
    }
}
